package cn.lingyangwl.agile.system.model.entity;

import cn.lingyangwl.agile.model.BasePlusPO;
import cn.lingyangwl.agile.tenant.core.DataIsolation;
import com.alibaba.fastjson2.JSON;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.handlers.AbstractJsonTypeHandler;
import lombok.Data;
import lombok.Getter;
import lombok.Setter;

import java.util.List;

/**
 * 文章
 *
 * @author shenguangyang
 */
@Getter
@Setter
@TableName(value = "article", autoResultMap = true)
@DataIsolation(filterTenant = true, tableName = "article")
public class Article extends BasePlusPO {
    @TableId
	private Long id;
	
	/**
	 * 文章组id
	 */
	private Long groupId;

	/**
	 * 文章标识
	 */
	private String code;
	
	/**
	 * 文章标题
	 */
	private String title;
	
	/**
	 * 文章内容
	 */
	private String content;
	
	/**
	 * 文章封面
	 */
	private String image;
	
	/**
	 * 访问数
	 */
	private Integer visit;
	
	/**
	 * 虚拟访问数
	 */
	private Integer visitVirtual;
	
	/**
	 * 状态
	 */
	private String status;
	
	/**
	 * 排序
	 */
	private Integer sortNo;

	/**
	 * 附件地址
	 */
	@TableField(typeHandler = AttachmentTypeHandler.class)
	private List<Attachment> attachments;

	public static class AttachmentTypeHandler extends AbstractJsonTypeHandler<List<Attachment>> {

		@Override
		protected List<Attachment> parse(String json) {
			return JSON.parseArray(json, Attachment.class);
		}

		@Override
		protected String toJson(List<Attachment> obj) {
			return JSON.toJSONString(obj);
		}
	}

	/**
	 * 附件信息
	 */
	@Data
	public static class Attachment {
		/**
		 * 文件名
		 */
		private String name;
		/**
		 * 文件url
		 */
		private String url;
	}

    public static LambdaQueryWrapper<Article> lqw() {
		return Wrappers.lambdaQuery();
	}
}
